package me.zhangjin.lucene.demo.queries;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.search.vectorhighlight.FastVectorHighlighter;
import org.apache.lucene.search.vectorhighlight.FieldQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class WildcardQueryTest {
    public static void main(String[] args) throws IOException {

        Path indexPath = Paths.get("indexdir/index");
        Directory dir = FSDirectory.open(indexPath);
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher searcher = new IndexSearcher(reader);

        // 通配符放在 text 属性中
        Term term = new Term("content", "学*");
        // 构造成一个 WildcardQuery
        Query wildcardQuery = new WildcardQuery(term);

        // Query:content:学?
        System.out.println("Query:" + wildcardQuery);

        // 返回前10条
        TopDocs tds = searcher.search(wildcardQuery, 10);
        for (ScoreDoc sd : tds.scoreDocs) {
            Explanation explanation = searcher.explain(wildcardQuery, sd.doc);
            System.out.println("explain:" + explanation + "\n");
            Document doc = searcher.doc(sd.doc);
            System.out.println("DocID:" + sd.doc);
            System.out.println("id:" + doc.get("id"));
            System.out.println("title:" + doc.get("title"));
            System.out.println("文档评分:" + sd.score);
        }
        dir.close();
        reader.close();
    }
}
